package com.example.questionexam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.constant.CommonConstant;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.mapper.AnswerMapper;
import com.example.questionexam.mapper.QuestionMapper;
import com.example.questionexam.model.dto.question.QuestionQueryRequest;
import com.example.questionexam.model.entity.Answer;
import com.example.questionexam.model.entity.Question;
import com.example.questionexam.model.enums.QuestionDifficultyEnum;
import com.example.questionexam.model.vo.QuestionVO;
import com.example.questionexam.service.QuestionService;
import com.example.questionexam.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author cola
* @description 针对表【question(题目)】的数据库操作Service实现
* @createDate 2024-05-27 19:27:21
*/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
    implements QuestionService{
    
    @Resource
    private AnswerMapper answerMapper;

    @Override
    public QueryWrapper<Question> getQueryWrapper(QuestionQueryRequest questionQueryRequest) {
        if (questionQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = questionQueryRequest.getId();
        String questionType = questionQueryRequest.getQuestionType();
        String knowledgePoint = questionQueryRequest.getKnowledgePoint();
        Integer difficulty = questionQueryRequest.getDifficulty();
        String chapter = questionQueryRequest.getChapter();
        String questionContent = questionQueryRequest.getQuestionContent();
        String sortField = questionQueryRequest.getSortField();
        String sortOrder = questionQueryRequest.getSortOrder();
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null && id != 0, "id", id);
        queryWrapper.eq(StringUtils.isNotBlank(questionType), "questionType", questionType);
        queryWrapper.eq(difficulty != null, "difficulty", difficulty);
        queryWrapper.eq(StringUtils.isNotBlank(chapter), "chapter", chapter);
        queryWrapper.like(StringUtils.isNotBlank(knowledgePoint), "knowledgePoint", knowledgePoint);
        queryWrapper.like(StringUtils.isNotBlank(questionContent), "questionContent", questionContent);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public void validQuestion(Question question, boolean isAdd) {
        ThrowUtils.throwIf(question == null, ErrorCode.PARAMS_ERROR);
        Long id = question.getId();
        String questionType = question.getQuestionType();
        String questionContent = question.getQuestionContent();
        String knowledgePoint = question.getKnowledgePoint();
        Integer difficulty = question.getDifficulty();
        String chapter = question.getChapter();
        String errorProne = question.getErrorProne();
        QuestionDifficultyEnum enumByValue = QuestionDifficultyEnum.getEnumByValue(difficulty);
        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(questionType,questionContent), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isNotBlank(knowledgePoint) && knowledgePoint.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "知识点字数过长");
        }
        if (StringUtils.isNotBlank(errorProne) && errorProne.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "易错点字数过长");
        }
        if (StringUtils.isNotBlank(chapter) && chapter.length() > 10) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "章节字数过长");
        }
        if (ObjectUtils.isNotEmpty(enumByValue) && !QuestionDifficultyEnum.SIMPLE.equals(enumByValue) && !QuestionDifficultyEnum.ORDINARY.equals(enumByValue) && !QuestionDifficultyEnum.DIFFICULT.equals(enumByValue)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目类型错误");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeQuestion(Long id) {
        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean removeQuestionResult = this.removeById(id);
        if (!removeQuestionResult) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        answerQueryWrapper.eq("questionId", id);
        // 查询是否存在答案
        Answer answer = answerMapper.selectOne(answerQueryWrapper);
        // 存在答案
        if (ObjectUtils.isNotEmpty(answer)) {
            int delete = answerMapper.delete(answerQueryWrapper);
            ThrowUtils.throwIf(delete == 0, ErrorCode.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public QuestionVO getQuestionWithAnswer(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询题目
        Question question = this.getById(id);
        QuestionVO questionVO = QuestionVO.objToVo(question);
        // 查询答案
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        answerQueryWrapper.eq("questionId", id);
        Answer answer = answerMapper.selectOne(answerQueryWrapper);
        if (ObjectUtils.isNotEmpty(answer)) {
            questionVO.setSolution(answer.getSolution());
            questionVO.setPicture(answer.getPicture());
        }
        return questionVO;
    }
}




